package com.orphan.audit.service.impl;

import com.orphan.audit.dao.SaveMapper;
import com.orphan.audit.po.Deposit;
import com.orphan.audit.po.Save;
import com.orphan.audit.service.IDepositService;
import com.orphan.audit.service.ISaveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orphan.util.dto.DepositDto;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wdn
 * @since 2021-07-15
 */
@Service
public class SaveServiceImpl extends ServiceImpl<SaveMapper, Save> implements ISaveService {

    @Resource
    private SaveMapper saveMapper;
    @Resource
    private IDepositService depositService;

    @Override
    public List<Save> findAll() {
        return saveMapper.findAll();
    }


    @Override
    public Integer saveMoney(Save save) {
        //存钱之前调用用户累计预存款表的save方法,看是否为新用户
        Integer integer = depositService.saveMoney(save);
        if(integer > 0) {
            //返回值0,累计预存款表存钱成功,在存款操作表保存save操作记录
            return saveMapper.insert(save);
        }
        return 0;
    }

//    @RabbitListener(queues = "deadQueue2")
//    public void getDepositMQ(DepositDto dto, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
//        Save save = new Save();
//        save.setTUserid((int)dto.getUId());
//        save.setTSum((float)dto.getMoney());
//        save.setTStuff("张三");
//        save.setTTime(LocalDateTime.now());
//        System.out.println(dto);
//        this.saveMoney(save);
//        //手动确认  false--不批量确认
//        channel.basicAck(tag,false);
//
//
//    }

    @RabbitListener(queues = "userDepositQueue")
    public void getMQMessage(DepositDto dto, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
        Save save = new Save();
        save.setTUserid(dto.getUId());
        save.setTSum((float)dto.getMoney());
        save.setTStuff("张三");
        save.setTTime(LocalDateTime.now());
        System.out.println(dto);

        this.saveMoney(save);
        //手动确认  false--不批量确认
        channel.basicAck(tag,false);

    }


}
